<template>
  <div>
    <div class="gva-search-box">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline" @keyup.enter="onSubmit">
      <el-form-item label="创建时间">
      <el-date-picker v-model="searchInfo.startCreatedAt" type="datetime" placeholder="开始时间"></el-date-picker>
       —
      <el-date-picker v-model="searchInfo.endCreatedAt" type="datetime" placeholder="结束时间"></el-date-picker>
      </el-form-item>
        <el-form-item label="编号">
         <el-input v-model="searchInfo.number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="制造序列号">
         <el-input v-model="searchInfo.manufacturing_serial_number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="排故日期">
            
           <el-date-picker v-model="searchInfo.troubleshooting_date" type="datetime" placeholder="搜索条件"></el-date-picker>

        </el-form-item>
        <el-form-item label="故障对应的工艺设计文件号">
         <el-input v-model="searchInfo.process_design_document_number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="负责工艺员">
         <el-input v-model="searchInfo.responsible_craftman" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="故障描述">
         <el-input v-model="searchInfo.fault_description" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="DISR号">
         <el-input v-model="searchInfo.dISR_number" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述1">
         <el-input v-model="searchInfo.operation_description1" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述2">
         <el-input v-model="searchInfo.operation_description2" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述3">
         <el-input v-model="searchInfo.operation_description3" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述4">
         <el-input v-model="searchInfo.operation_description4" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="操作描述5">
         <el-input v-model="searchInfo.operation_description5" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="ATA章节（一级）">
         <el-input v-model="searchInfo.ata_chapter_level1" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="ATA章节（二级）">
         <el-input v-model="searchInfo.ata_chapter_level2" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="结论">
         <el-input v-model="searchInfo.conclusion" placeholder="搜索条件" />

        </el-form-item>
        <el-form-item label="完成时间">
            
           <el-date-picker v-model="searchInfo.complete_date" type="datetime" placeholder="搜索条件"></el-date-picker>

        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
        <div class="gva-btn-list">
            <el-button type="primary" icon="plus" @click="openDialog">新增</el-button>
            <el-popover v-model:visible="deleteVisible" placement="top" width="160">
            <p>确定要删除吗？</p>
            <div style="text-align: right; margin-top: 8px;">
                <el-button type="primary" link @click="deleteVisible = false">取消</el-button>
                <el-button type="primary" @click="onDelete">确定</el-button>
            </div>
            <template #reference>
                <el-button icon="delete" style="margin-left: 10px;" :disabled="!multipleSelection.length" @click="deleteVisible = true">删除</el-button>
            </template>
            </el-popover>
        </div>
        <el-table
        ref="multipleTable"
        style="width: 100%"
        tooltip-effect="dark"
        :data="tableData"
        row-key="ID"
        @selection-change="handleSelectionChange"
        >
        <el-table-column type="selection" width="55" />
        <el-table-column align="left" label="日期" width="180">
            <template #default="scope">{{ formatDate(scope.row.CreatedAt) }}</template>
        </el-table-column>
        <el-table-column align="left" label="编号" prop="number" width="120" />
        <el-table-column align="left" label="制造序列号" prop="manufacturing_serial_number" width="120" />
         <el-table-column align="left" label="排故日期" width="180">
            <template #default="scope">{{ formatDate(scope.row.troubleshooting_date) }}</template>
         </el-table-column>
        <el-table-column align="left" label="故障对应的工艺设计文件号" prop="process_design_document_number" width="120" />
        <el-table-column align="left" label="负责工艺员" prop="responsible_craftman" width="120" />
        <el-table-column align="left" label="故障描述" prop="fault_description" width="120" />
        <el-table-column align="left" label="DISR号" prop="dISR_number" width="120" />
        <el-table-column align="left" label="序号1" prop="serial_number1" width="120" />
        <el-table-column align="left" label="操作描述1" prop="operation_description1" width="120" />
        <el-table-column align="left" label="是否需要质量检查1" prop="is_quality_inspection_required1" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required1) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号2" prop="serial_number2" width="120" />
        <el-table-column align="left" label="操作描述2" prop="operation_description2" width="120" />
        <el-table-column align="left" label="是否需要质量测查2" prop="is_quality_inspection_required2" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required2) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号3" prop="serial_number3" width="120" />
        <el-table-column align="left" label="操作描述3" prop="operation_description3" width="120" />
        <el-table-column align="left" label="是否需要质量检测3" prop="is_quality_inspection_required3" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required3) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号4" prop="serial_number4" width="120" />
        <el-table-column align="left" label="操作描述4" prop="operation_description4" width="120" />
        <el-table-column align="left" label="是否需要质量检查4" prop="is_quality_inspection_required4" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required4) }}</template>
        </el-table-column>
        <el-table-column align="left" label="序号5" prop="serial_number5" width="120" />
        <el-table-column align="left" label="操作描述5" prop="operation_description5" width="120" />
        <el-table-column align="left" label="是否需要质量检测5" prop="is_quality_inspection_required5" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_quality_inspection_required5) }}</template>
        </el-table-column>
        <el-table-column align="left" label="ATA章节（一级）" prop="ata_chapter_level1" width="120" />
        <el-table-column align="left" label="ATA章节（二级）" prop="ata_chapter_level2" width="120" />
        <el-table-column align="left" label="纸质排故单上传" prop="upload_troubleshooting_form" width="120" />
        <el-table-column align="left" label="附件上传" prop="upload_attachment" width="120" />
        <el-table-column align="left" label="结论" prop="conclusion" width="120" />
         <el-table-column align="left" label="完成时间" width="180">
            <template #default="scope">{{ formatDate(scope.row.complete_date) }}</template>
         </el-table-column>
        <el-table-column align="left" label="是否通过" prop="is_passed" width="120">
            <template #default="scope">{{ formatBoolean(scope.row.is_passed) }}</template>
        </el-table-column>
        <el-table-column align="left" label="操作">
            <template #default="scope">
            <el-button type="primary" link icon="edit" class="table-button" @click="updateCase_struct_backupFunc(scope.row)">变更</el-button>
            <el-button type="primary" link icon="delete" @click="deleteRow(scope.row)">删除</el-button>
            </template>
        </el-table-column>
        </el-table>
        <div class="gva-pagination">
            <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            />
        </div>
    </div>
    <el-dialog v-model="dialogFormVisible" :before-close="closeDialog" title="弹窗操作">
      <el-form :model="formData" label-position="right" ref="elFormRef" :rules="rule" label-width="80px">
        <el-form-item label="编号:"  prop="number" >
          <el-input v-model="formData.number" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="制造序列号:"  prop="manufacturing_serial_number" >
          <el-input v-model="formData.manufacturing_serial_number" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="排故日期:"  prop="troubleshooting_date" >
          <el-date-picker v-model="formData.troubleshooting_date" type="date" style="width:100%" placeholder="选择日期" :clearable="true"  />
        </el-form-item>
        <el-form-item label="故障对应的工艺设计文件号:"  prop="process_design_document_number" >
          <el-input v-model="formData.process_design_document_number" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="负责工艺员:"  prop="responsible_craftman" >
          <el-input v-model="formData.responsible_craftman" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="故障描述:"  prop="fault_description" >
          <el-input v-model="formData.fault_description" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="DISR号:"  prop="dISR_number" >
          <el-input v-model="formData.dISR_number" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="序号1:"  prop="serial_number1" >
          <el-input v-model.number="formData.serial_number1" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="操作描述1:"  prop="operation_description1" >
          <el-input v-model="formData.operation_description1" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="是否需要质量检查1:"  prop="is_quality_inspection_required1" >
          <el-switch v-model="formData.is_quality_inspection_required1" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
        </el-form-item>
        <el-form-item label="序号2:"  prop="serial_number2" >
          <el-input v-model.number="formData.serial_number2" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="操作描述2:"  prop="operation_description2" >
          <el-input v-model="formData.operation_description2" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="是否需要质量测查2:"  prop="is_quality_inspection_required2" >
          <el-switch v-model="formData.is_quality_inspection_required2" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
        </el-form-item>
        <el-form-item label="序号3:"  prop="serial_number3" >
          <el-input v-model.number="formData.serial_number3" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="操作描述3:"  prop="operation_description3" >
          <el-input v-model="formData.operation_description3" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="是否需要质量检测3:"  prop="is_quality_inspection_required3" >
          <el-switch v-model="formData.is_quality_inspection_required3" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
        </el-form-item>
        <el-form-item label="序号4:"  prop="serial_number4" >
          <el-input v-model.number="formData.serial_number4" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="操作描述4:"  prop="operation_description4" >
          <el-input v-model="formData.operation_description4" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="是否需要质量检查4:"  prop="is_quality_inspection_required4" >
          <el-switch v-model="formData.is_quality_inspection_required4" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
        </el-form-item>
        <el-form-item label="序号5:"  prop="serial_number5" >
          <el-input v-model.number="formData.serial_number5" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="操作描述5:"  prop="operation_description5" >
          <el-input v-model="formData.operation_description5" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="是否需要质量检测5:"  prop="is_quality_inspection_required5" >
          <el-switch v-model="formData.is_quality_inspection_required5" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
        </el-form-item>
        <el-form-item label="ATA章节（一级）:"  prop="ata_chapter_level1" >
          <el-input v-model="formData.ata_chapter_level1" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="ATA章节（二级）:"  prop="ata_chapter_level2" >
          <el-input v-model="formData.ata_chapter_level2" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="纸质排故单上传:"  prop="upload_troubleshooting_form" >
          <el-input v-model="formData.upload_troubleshooting_form" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="附件上传:"  prop="upload_attachment" >
          <el-input v-model="formData.upload_attachment" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="结论:"  prop="conclusion" >
          <el-input v-model="formData.conclusion" :clearable="true"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="完成时间:"  prop="complete_date" >
          <el-date-picker v-model="formData.complete_date" type="date" style="width:100%" placeholder="选择日期" :clearable="true"  />
        </el-form-item>
        <el-form-item label="是否通过:"  prop="is_passed" >
          <el-switch v-model="formData.is_passed" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="enterDialog">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Case_struct_backup'
}
</script>

<script setup>
import {
  createCase_struct_backup,
  deleteCase_struct_backup,
  deleteCase_struct_backupByIds,
  updateCase_struct_backup,
  findCase_struct_backup,
  getCase_struct_backupList
} from '@/api/sysCaseLibrary_backup'

// 全量引入格式化工具 请按需保留
import { getDictFunc, formatDate, formatBoolean, filterDict } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive } from 'vue'

// 自动化生成的字典（可能为空）以及字段
const formData = ref({
        number: '',
        manufacturing_serial_number: '',
        troubleshooting_date: new Date(),
        process_design_document_number: '',
        responsible_craftman: '',
        fault_description: '',
        dISR_number: '',
        serial_number1: 0,
        operation_description1: '',
        is_quality_inspection_required1: false,
        serial_number2: 0,
        operation_description2: '',
        is_quality_inspection_required2: false,
        serial_number3: 0,
        operation_description3: '',
        is_quality_inspection_required3: false,
        serial_number4: 0,
        operation_description4: '',
        is_quality_inspection_required4: false,
        serial_number5: 0,
        operation_description5: '',
        is_quality_inspection_required5: false,
        ata_chapter_level1: '',
        ata_chapter_level2: '',
        upload_troubleshooting_form: '',
        upload_attachment: '',
        conclusion: '',
        complete_date: new Date(),
        is_passed: false,
        })

// 验证规则
const rule = reactive({
               manufacturing_serial_number : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               troubleshooting_date : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               process_design_document_number : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               responsible_craftman : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               fault_description : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               ata_chapter_level1 : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               ata_chapter_level2 : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()


// =========== 表格控制部分 ===========
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})

// 重置
const onReset = () => {
  searchInfo.value = {}
  getTableData()
}

// 搜索
const onSubmit = () => {
  page.value = 1
  pageSize.value = 10
  if (searchInfo.value.is_quality_inspection_required1 === ""){
      searchInfo.value.is_quality_inspection_required1=null
  }
  if (searchInfo.value.is_quality_inspection_required2 === ""){
      searchInfo.value.is_quality_inspection_required2=null
  }
  if (searchInfo.value.is_quality_inspection_required3 === ""){
      searchInfo.value.is_quality_inspection_required3=null
  }
  if (searchInfo.value.is_quality_inspection_required4 === ""){
      searchInfo.value.is_quality_inspection_required4=null
  }
  if (searchInfo.value.is_quality_inspection_required5 === ""){
      searchInfo.value.is_quality_inspection_required5=null
  }
  if (searchInfo.value.is_passed === ""){
      searchInfo.value.is_passed=null
  }
  getTableData()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

// 修改页面容量
const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await getCase_struct_backupList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

getTableData()

// ============== 表格控制部分结束 ===============

// 获取需要的字典 可能为空 按需保留
const setOptions = async () =>{
}

// 获取需要的字典 可能为空 按需保留
setOptions()


// 多选数据
const multipleSelection = ref([])
// 多选
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

// 删除行
const deleteRow = (row) => {
    ElMessageBox.confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
            deleteCase_struct_backupFunc(row)
        })
    }


// 批量删除控制标记
const deleteVisible = ref(false)

// 多选删除
const onDelete = async() => {
      const ids = []
      if (multipleSelection.value.length === 0) {
        ElMessage({
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
      multipleSelection.value &&
        multipleSelection.value.map(item => {
          ids.push(item.ID)
        })
      const res = await deleteCase_struct_backupByIds({ ids })
      if (res.code === 0) {
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
        if (tableData.value.length === ids.length && page.value > 1) {
          page.value--
        }
        deleteVisible.value = false
        getTableData()
      }
    }

// 行为控制标记（弹窗内部需要增还是改）
const type = ref('')

// 更新行
const updateCase_struct_backupFunc = async(row) => {
    const res = await findCase_struct_backup({ ID: row.ID })
    type.value = 'update'
    if (res.code === 0) {
        formData.value = res.data.recase_backup
        dialogFormVisible.value = true
    }
}


// 删除行
const deleteCase_struct_backupFunc = async (row) => {
    const res = await deleteCase_struct_backup({ ID: row.ID })
    if (res.code === 0) {
        ElMessage({
                type: 'success',
                message: '删除成功'
            })
            if (tableData.value.length === 1 && page.value > 1) {
            page.value--
        }
        getTableData()
    }
}

// 弹窗控制标记
const dialogFormVisible = ref(false)

// 打开弹窗
const openDialog = () => {
    type.value = 'create'
    dialogFormVisible.value = true
}

// 关闭弹窗
const closeDialog = () => {
    dialogFormVisible.value = false
    formData.value = {
        number: '',
        manufacturing_serial_number: '',
        troubleshooting_date: new Date(),
        process_design_document_number: '',
        responsible_craftman: '',
        fault_description: '',
        dISR_number: '',
        serial_number1: 0,
        operation_description1: '',
        is_quality_inspection_required1: false,
        serial_number2: 0,
        operation_description2: '',
        is_quality_inspection_required2: false,
        serial_number3: 0,
        operation_description3: '',
        is_quality_inspection_required3: false,
        serial_number4: 0,
        operation_description4: '',
        is_quality_inspection_required4: false,
        serial_number5: 0,
        operation_description5: '',
        is_quality_inspection_required5: false,
        ata_chapter_level1: '',
        ata_chapter_level2: '',
        upload_troubleshooting_form: '',
        upload_attachment: '',
        conclusion: '',
        complete_date: new Date(),
        is_passed: false,
        }
}
// 弹窗确定
const enterDialog = async () => {
     elFormRef.value?.validate( async (valid) => {
             if (!valid) return
              let res
              switch (type.value) {
                case 'create':
                  res = await createCase_struct_backup(formData.value)
                  break
                case 'update':
                  res = await updateCase_struct_backup(formData.value)
                  break
                default:
                  res = await createCase_struct_backup(formData.value)
                  break
              }
              if (res.code === 0) {
                ElMessage({
                  type: 'success',
                  message: '创建/更改成功'
                })
                closeDialog()
                getTableData()
              }
      })
}
</script>

<style>
</style>
